package com.huitone.gddw.controller.worktable.paramconfigmanage;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.VirtualGateLink;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IVirtualGateLinkService;
import com.huitone.gddw.service.dto.VirtualGateLinkDTO;
import com.huitone.gddw.utils.SysLoginUtil;

@Controller
@RequestMapping(value="virtualGateLink")
public class VirtualGateLinkController extends CommonController {

	private final  static Logger LOGGER = Logger.getLogger(VirtualGateLinkController.class);
	
	@Autowired
	private IVirtualGateLinkService virtualGateLinkService;
	
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<VirtualGateLinkDTO> query(QueryPage<VirtualGateLink> page,HttpServletRequest request){
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询虚拟通道信息";
		Map<String, Object> params = new HashMap<String, Object>();
		String gateLinkId = request.getParameter("gateLinkId");
		String virtualGateLinkId = request.getParameter("virtualGateLinkId");
		String status = request.getParameter("status");
		params.put("gateLinkId", gateLinkId);
		params.put("virtualGateLinkId", virtualGateLinkId);
		params.put("status", status);
		page.setParams(params);
		
		QueryPage<VirtualGateLinkDTO> result = null;
		try {
			result = virtualGateLinkService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询虚拟通道信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(VirtualGateLinkDTO entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加虚拟通道信息";
		Message message = new Message(true, "保存成功！");
		try {
			message = virtualGateLinkService.save(entity);
		} catch (Exception e) {
			LOGGER.error("保存拟通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			message.setSuccess(false);
			message.setMsg("保存失败！");
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return message;
	}
	
	@ResponseBody
	@RequestMapping(value="toUpdate")
	private VirtualGateLinkDTO toUpdate(String gateLinkId){
		try {
			VirtualGateLinkDTO entity = virtualGateLinkService.toUpdate(gateLinkId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new VirtualGateLinkDTO();
		}
	}
	
	@ResponseBody
	@RequestMapping("update")
	private Message update(VirtualGateLinkDTO entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改虚拟通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			Message message = virtualGateLinkService.update(entity);
			if (!message.isSuccess) {
				return message;
			}
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改拟通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	@ResponseBody
	@RequestMapping(value="delete")
	private Message delete(String gateLinkId) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除虚拟通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			virtualGateLinkService.delete(gateLinkId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除拟通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
}
